home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 557 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: slip-d8.rdrop.com!user
  2. From: pcal@agora.rdrop.com (Patrick Calahan)
  3. Newsgroups: comp.lang.c++,comp.sys.mac.programmer.codewarrior,comp.sys.mac.oop
  4. Subject: Help needed with Runtime Type Info
  5. Date: Thu, 04 Jan 1996 20:39:48 -0800
  6. Organization: RainDrop Laboratories/Agora(sm)
  7. Message-ID: <pcal-0401962039480001@slip-d8.rdrop.com>
  8. NNTP-Posting-Host: slip-d8.rdrop.com
  9.  
  10.  
  11.      I need a little assistance from a C++ guru.
  12.      
  13.      I have an abstract class B from which D,E and F are derived
  14.      
  15.      I also have an abstract class O from which P, Q and R are derived.
  16.      
  17.      Objects derived from O need to perform specific operations given a 
  18.      pair of pointers to B's. The operation to be performed depends on 
  19.      precisely  what kind of B's are given to the O.
  20.      
  21.      I thought that by simply overloading member function f in the O's,
  22.      I could achieve this affect nicely - f(D*,D*), f(D*,E*), f(E*,F*)...
  23.      could be defined to handle the particualr pair-cases appropriately.
  24.      A member function in O f(B*,B*) would handle all cases not handled
  25.      specifically by the subclasses.
  26.      
  27.      The only problem is figuring out exactly what type of object a B* is
  28.      pointing to so that the appropriate call to O.f can be made.  I.e.,
  29.      given a pointer to a B, I need to cast it to a D*,E*, or F*.
  30.      
  31.      On page 640 of Stroustrup, he says that [in dynamic_cast<t>(v)]
  32.      "If T is void* then v must be a pointer, and the result value is a
  33.      pointer to the complete object pointed to by v."
  34.  
  35.      So, if I read him correctly,
  36.      
  37.      dynamic_cast<void*>(p*)
  38.      
  39.      should cast p to a pointer to an object of the most complete type of 
  40.      the object pointed to by p, i.e. cast my B* to an D*, E* or F*.
  41.      This sounds like what I want.
  42.      
  43.      Unfortunately, if I try to compile the following using MetroWerks
  44.      CodeWarrior 7 (I do have RTTI enabled):
  45.      
  46.      
  47.      some_function(B* b1, B*b2, O* o)
  48.      {
  49.  
  50.      // here I need to figure out what types of objects b1 & b2 really are
  51.      // and cast the pointers so the correct version of f will be called.
  52.      
  53.      o->f( dynamic_cast<void*>(b1), dynamic_cast<void*>(b2) );
  54.      
  55.      }
  56.      
  57.      
  58.      b1 and b2 apparently get cast to void*; the compiler returns that
  59.       f(void*,void*) can't be found.
  60.      
  61.      What do I need to do here?
  62.      
  63.      
  64.      I'm relatively new to C++, so I might just be looking at the problem
  65.      incorrectly.
  66.      
  67.      I tried to do it just using virtual functions in B, but the fact that
  68.      I'm dealing with pairs of B's in the O's seems to make this less 
  69.      elegant.
  70.      
  71.      Moreover, it makes more sense in my application for O to be a separate 
  72.      class.
  73.  
  74.  
  75.      Many many thanks in advance for any help,
  76.  
  77.       -p
  78. -- 
  79. Patrick Calahan                          pcal@agora.rdrop.com
  80. Portland, Oregon                   http://www.rdrop.com/~pcal
  81.